﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoShopping;
using GoShopping.BLL;
using GoShopping.Model;
using GoShopping.Model.Enumerations;
using System.Web.Security;
using System.Net.Mail;
using System.Transactions;

namespace GoShopping.Web
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["userName"]))
                {
                    GoShopping.BLL.UserBean ub = new UserBean();
                    GoShopping.Model.UserInfo user = ub.GetOneUser(Request.QueryString["userName"].ToString());
                    //当用户名存在时,返回False提示用户此用户名已存在
                    //否则可以注册
                    if (user != null)
                    {
                        Response.Expires = -1;
                        Response.ContentType = "text/xml";
                        string str = "<check>" +
                            "<result>false</result>" +
                            "</check>";
                        Response.Write(str);
                        Response.End();
                    }
                    else
                    {
                        Response.Expires = -1;
                        Response.ContentType = "text/xml";
                        string str = "<check>" +
                            "<result>true</result>" +
                            "</check>";
                        Response.Write(str);
                        Response.End();
                    }
                }
            }
        }
        protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {

            MemberShipService memberShaipService = new MemberShipService();
            bool isuser = memberShaipService.IsMembershipDBHasData();

            string UserName = CreateUserWizard1.UserName.ToString();   //用户名称
            string Email = CreateUserWizard1.Email.ToString();      //电子邮件
            string PassWord = CreateUserWizard1.Password.ToString();   //密码
            string passwordQustion = CreateUserWizard1.Question.Trim();  //密码提示问题
            string passwordanser = CreateUserWizard1.Answer.Trim();     //密码提示答案            

            MembershipCreateStatus statue;

            if (isuser)//数据库中已经有用户数据了，这时候添加的用户就不可能是超级管理员了
            {
                bool createUser = false;
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    createUser = memberShaipService.CreateMember(UserName, Email, PassWord, passwordQustion, passwordanser, true, Member_Roles.Role_Member, out statue);
                    ts.Complete();
                }
                if (createUser)
                {
                    //Utility.MailUtility mu = new GoShopping.Utility.MailUtility();
                    //mu.SetMail(Email, "Welcome to Goshopping world", string.Format("Welcome {0}, wish to give you a better experience", UserName));
                    //SendMail(UserName, Email);
                    LogToMail(UserName, Email);
                    //会员注册成功，页面跳转到index页面
                    Response.Redirect("Login.aspx");
                }
                else
                {
                    //注册不成功，输出错误信息，清空注册密码。
                    lbinfo.Visible = true;
                    switch (statue.ToString())
                    {
                        case "DuplicateEmail":
                            lbinfo.Text = "电子邮件地址已存在于应用程序的数据库中";
                            break;
                        case "DuplicateProviderUserKey":
                            lbinfo.Text = "提供程序用户键值已存在于应用程序的数据库中";
                            break;
                        case "DuplicateUserName":
                            lbinfo.Text = "用户名已存在于应用程序的数据库中";
                            break;
                        case "InvalidAnswer":
                            lbinfo.Text = "密码提示问题答案的格式设置不正确";
                            break;
                        case "InvalidEmail":
                            lbinfo.Text = "电子邮件地址的格式设置不正确";
                            break;
                        case "InvalidPassword":
                            lbinfo.Text = "密码的格式设置不正确";
                            break;
                        case "InvalidProviderUserKey":
                            lbinfo.Text = "提供程序用户键值的类型或格式无效";
                            break;
                        case "InvalidQuestion":
                            lbinfo.Text = "密码提示问题的格式设置不正确";
                            break;
                        case "InvalidUserName":
                            lbinfo.Text = "在数据库中未找到用户名";
                            break;
                        case "ProviderError":
                            lbinfo.Text = "提供程序返回一个未由其他 MembershipCreateStatus 枚举值描述的错误";
                            break;
                        case "Success":
                            lbinfo.Text = "创建用户成功";
                            break;
                        case "UserRejected":
                            lbinfo.Text = "用户不成功,请稍后再试!";
                            break;
                    }

                }

            }
        }

        private static void LogToMail(string UserName, string Email)
        {
            GoShopping.BLL.SysInfo.Log_EmailSendListBean log_Mail = new GoShopping.BLL.SysInfo.Log_EmailSendListBean();
            GoShopping.Model.Log_EmailSendList log = new Log_EmailSendList();
            EmailTempleteBean etb = new EmailTempleteBean();
            Utility.MailSettings ms = new Utility.MailUtility().GetSmtpSettings();
            log.CreateDate = DateTime.Now;
            log.FromEmail = ms.MailFrom;
            log.ToUserEmail = Email;
            log.EmailTemplateName = Model.Enumerations.EmailTempleteType.UserRegist.ToString();
            try
            {
                EmailTemplete et = etb.GetEmailTemplete(EmailTempleteType.UserRegist);
                log.EmailContent = string.Format(et.ContentHTML, new string[] { UserName, "Zhanglei" });
                log_Mail.Add(log);
            }
            catch
            {
                return;
            }
        }
        //protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        //{

        //    MemberShipService memberShaipService = new MemberShipService();
        //    bool isuser = memberShaipService.IsMembershipDBHasData();

        //    string UserName = CreateUserWizard1.UserName.ToString();   //用户名称
        //    string Email = CreateUserWizard1.Email.ToString();      //电子邮件
        //    string PassWord = CreateUserWizard1.Password.ToString();   //密码
        //    string passwordQustion = CreateUserWizard1.Question.Trim();  //密码提示问题
        //    string passwordanser = CreateUserWizard1.Answer.Trim();     //密码提示答案            

        //    MembershipCreateStatus statue;

        //    if (isuser)//数据库中已经有用户数据了，这时候添加的用户就不可能是超级管理员了
        //    {
        //        bool createUser = memberShaipService.CreateMember(UserName, Email, PassWord, passwordQustion, passwordanser, true, Member_Roles.Role_Member, out statue);
        //        if (createUser)
        //        {
        //            //SendMail(UserName, Email);
        //            //会员注册成功，页面跳转到index页面
        //            Response.Redirect("Login.aspx?UserName=" + UserName);
        //        }
        //        else
        //        {
        //            //注册不成功，输出错误信息，清空注册密码。
        //            lbinfo.Visible = true;
        //            switch (statue.ToString())
        //            {
        //                case "DuplicateEmail":
        //                    lbinfo.Text = "电子邮件地址已存在于应用程序的数据库中";
        //                    break;
        //                case "DuplicateProviderUserKey":
        //                    lbinfo.Text = "提供程序用户键值已存在于应用程序的数据库中";
        //                    break;
        //                case "DuplicateUserName":
        //                    lbinfo.Text = "用户名已存在于应用程序的数据库中";
        //                    break;
        //                case "InvalidAnswer":
        //                    lbinfo.Text = "密码提示问题答案的格式设置不正确";
        //                    break;
        //                case "InvalidEmail":
        //                    lbinfo.Text = "电子邮件地址的格式设置不正确";
        //                    break;
        //                case "InvalidPassword":
        //                    lbinfo.Text = "密码的格式设置不正确";
        //                    break;
        //                case "InvalidProviderUserKey":
        //                    lbinfo.Text = "提供程序用户键值的类型或格式无效";
        //                    break;
        //                case "InvalidQuestion":
        //                    lbinfo.Text = "密码提示问题的格式设置不正确";
        //                    break;
        //                case "InvalidUserName":
        //                    lbinfo.Text = "在数据库中未找到用户名";
        //                    break;
        //                case "ProviderError":
        //                    lbinfo.Text = "提供程序返回一个未由其他 MembershipCreateStatus 枚举值描述的错误";
        //                    break;
        //                case "Success":
        //                    lbinfo.Text = "创建用户成功";
        //                    break;
        //                case "UserRejected":
        //                    lbinfo.Text = "用户不成功,请稍后再试!";
        //                    break;
        //            }

        //        }

        //    }
        //}
        //private void SendMail(string UserName, string Email)
        //{
        //    MailMessage mess = new MailMessage();
        //    mess.From = new MailAddress("rayer1@sina.com");
        //    //E-mail Address要正确
        //    mess.Subject = "恭喜你," + UserName + ",成为GoShopping一员";
        //    //邮件标题
        //    mess.IsBodyHtml = true;
        //    mess.BodyEncoding = System.Text.Encoding.UTF8;
        //    //邮件编码
        //    mess.Body = "想免费购物吗？赶快加入GoShopping！<br>QQ群号：1231231<br>机会宝贵，赶快加入!";
        //    //邮件正文
        //    SmtpClient client = new SmtpClient();
        //    client.Host = "smtp.sina.com";
        //    //SMTP服务器要正确，经测试，可以使用smtp.sina.com(.cn)    smtp.163.com   smtp.126.com
        //    client.Credentials = new System.Net.NetworkCredential("rayer1", "123qwe");
        //    //需要验证，用户名和密码要正确
        //    mess.To.Add(new MailAddress(Email));
        //    //接收邮件的邮箱要正确
        //    try
        //    {
        //        client.Send(mess);
        //        Response.Write("邮件发送到" + mess.To.ToString() + "<br>");
        //    }
        //    catch (Exception ee)
        //    {
        //        Response.Write(ee.Message + "<br>");
        //    }
        //}
    }
}
